<template>
  <div class="staff-manage-root">
    <!-- 顶部操作区 -->
    <el-form :inline="true" class="filter-bar" @submit.prevent>
      <el-input v-model="searchKeyword" placeholder="姓名/工号/手机号" style="width: 200px; margin-right: 12px;" clearable />
      <el-select v-model="searchStatus" placeholder="状态" style="width: 120px; margin-right: 12px;">
        <el-option label="全部" value="" />
        <el-option label="在岗" value="active" />
        <el-option label="离岗" value="inactive" />
        <el-option label="禁用" value="disabled" />
      </el-select>
      <el-button type="primary" @click="openEditDialog()">新增客服</el-button>
      <el-button>导入</el-button>
      <el-button>导出</el-button>
    </el-form>

    <!-- 客服列表 -->
    <el-table :data="filteredStaffList" border style="width: 100%; margin-top: 16px;">
      <el-table-column prop="staffNo" label="工号" width="100" />
      <el-table-column prop="name" label="姓名" width="120" />
      <el-table-column prop="mobile" label="手机号" width="140" />
      <el-table-column prop="groups" label="分组" width="160">
        <template #default="scope">
          <el-tag v-for="g in scope.row.groups" :key="g" size="small" style="margin-right: 4px;">{{ g }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="roles" label="角色" width="160">
        <template #default="scope">
          <el-tag v-for="r in scope.row.roles" :key="r" size="small" type="info" style="margin-right: 4px;">{{ r }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="getStatusTagType(scope.row.status)">{{ getStatusLabel(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="340">
        <template #default="scope">
          <el-button size="small" @click="openEditDialog(scope.row)">编辑</el-button>
          <el-button size="small" @click="resetPassword(scope.row)">重置密码</el-button>
          <el-button size="small" @click="openAssignDialog(scope.row)">分配分组/角色</el-button>
          <el-button size="small" type="warning" @click="toggleStatus(scope.row)">
            {{ scope.row.status === 'disabled' ? '启用' : '禁用' }}
          </el-button>
          <el-button size="small" type="danger" @click="deleteStaff(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑客服弹窗 -->
    <el-dialog v-model="editDialogVisible" :title="editMode === 'add' ? '新增客服' : '编辑客服'" width="500px">
      <el-form :model="editForm" label-width="80px">
        <el-form-item label="选择员工">
          <el-select v-model="editForm.employeeId" placeholder="请选择员工" @change="onEmployeeChange">
            <el-option v-for="emp in employeeList" :key="emp.id" :label="emp.name + '（' + emp.staffNo + '）'" :value="emp.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="editForm.mobile" readonly />
        </el-form-item>
        <el-form-item label="工号">
          <el-input v-model="editForm.staffNo" readonly />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveStaff">保存</el-button>
      </template>
    </el-dialog>

    <!-- 分配分组/角色弹窗 -->
    <el-dialog v-model="assignDialogVisible" title="分配分组/角色" width="400px">
      <el-form>
        <el-form-item label="分组">
          <el-select v-model="assignForm.groups" multiple placeholder="请选择分组">
            <el-option v-for="g in allGroups" :key="g" :label="g" :value="g" />
          </el-select>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="assignForm.roles" multiple placeholder="请选择角色">
            <el-option v-for="r in allRoles" :key="r" :label="r" :value="r" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="assignDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveAssign">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

interface Staff {
  id: string
  staffNo: string
  name: string
  mobile: string
  roles: string[]
  groups: string[]
  status: 'active' | 'inactive' | 'disabled'
  createTime: string
  remark?: string
}

// mock员工列表（假设从员工管理获取）
const employeeList = [
  { id: 'e1', name: '张三', mobile: '13800000001', staffNo: '1001' },
  { id: 'e2', name: '李四', mobile: '13800000002', staffNo: '1002' },
  { id: 'e3', name: '王五', mobile: '13800000003', staffNo: '1003' }
]

const allGroups = ['售前组', '售后组', '投诉组', 'VIP组']
const allRoles = ['普通客服', '组长', '主管']

const searchKeyword = ref('')
const searchStatus = ref('')
const staffList = ref<Staff[]>([
  { id: '1', staffNo: '1001', name: '张三', mobile: '13800000001', roles: ['普通客服'], groups: ['售前组'], status: 'active', createTime: '2024-06-01 10:00:00' },
  { id: '2', staffNo: '1002', name: '李四', mobile: '13800000002', roles: ['组长'], groups: ['售后组'], status: 'inactive', createTime: '2024-06-02 11:00:00' },
  { id: '3', staffNo: '1003', name: '王五', mobile: '13800000003', roles: ['主管'], groups: ['投诉组'], status: 'disabled', createTime: '2024-06-03 12:00:00' }
])

const filteredStaffList = computed(() => {
  return staffList.value.filter(item => {
    const matchKeyword = !searchKeyword.value || [item.name, item.staffNo, item.mobile].some(f => f.includes(searchKeyword.value))
    const matchStatus = !searchStatus.value || item.status === searchStatus.value
    return matchKeyword && matchStatus
  })
})

const editDialogVisible = ref(false)
const editMode = ref<'add' | 'edit'>('add')
const editForm = ref<any>({})

function openEditDialog(staff?: Staff) {
  if (staff) {
    editMode.value = 'edit'
    editForm.value = { ...staff, employeeId: '' }
  } else {
    editMode.value = 'add'
    editForm.value = { employeeId: '', name: '', mobile: '', staffNo: '', remark: '' }
  }
  editDialogVisible.value = true
}

function onEmployeeChange(empId: string) {
  const emp = employeeList.find(e => e.id === empId)
  if (emp) {
    editForm.value.name = emp.name
    editForm.value.mobile = emp.mobile
    editForm.value.staffNo = emp.staffNo
  }
}

function saveStaff() {
  if (!editForm.value.employeeId) {
    alert('请选择员工')
    return
  }
  if (editMode.value === 'add') {
    staffList.value.push({
      id: Date.now().toString(),
      staffNo: editForm.value.staffNo,
      name: editForm.value.name,
      mobile: editForm.value.mobile,
      roles: [],
      groups: [],
      status: 'active',
      createTime: new Date().toLocaleString(),
      remark: editForm.value.remark
    })
  } else {
    const idx = staffList.value.findIndex(s => s.id === editForm.value.id)
    if (idx > -1) {
      staffList.value[idx] = { ...staffList.value[idx], ...editForm.value }
    }
  }
  editDialogVisible.value = false
}

const assignDialogVisible = ref(false)
const assignForm = ref<{ id: string, groups: string[], roles: string[] }>({ id: '', groups: [], roles: [] })

function openAssignDialog(staff: Staff) {
  assignForm.value = { id: staff.id, groups: [...(staff.groups || [])], roles: [...(staff.roles || [])] }
  assignDialogVisible.value = true
}
function saveAssign() {
  const idx = staffList.value.findIndex(s => s.id === assignForm.value.id)
  if (idx > -1) {
    staffList.value[idx].groups = [...assignForm.value.groups]
    staffList.value[idx].roles = [...assignForm.value.roles]
  }
  assignDialogVisible.value = false
}

function resetPassword(staff: Staff) {
  alert(`已重置 ${staff.name} 的密码`)
}

function toggleStatus(staff: Staff) {
  staff.status = staff.status === 'disabled' ? 'active' : 'disabled'
}

function deleteStaff(staff: Staff) {
  if (confirm(`确定要删除客服 ${staff.name} 吗？`)) {
    staffList.value = staffList.value.filter(s => s.id !== staff.id)
  }
}

function getStatusTagType(status: Staff['status']) {
  if (status === 'active') return 'success'
  if (status === 'inactive') return 'info'
  return 'danger'
}
function getStatusLabel(status: Staff['status']) {
  if (status === 'active') return '在岗'
  if (status === 'inactive') return '离岗'
  return '禁用'
}
</script>

<style scoped>
.staff-manage-root {
  padding: 24px;
  background: #f5f5f5;
  min-height: 100vh;
}
.filter-bar {
  margin-bottom: 16px;
}
</style> 